//
//  WLStatusListViewModel.swift
//  WLWeibo
//
//  Created by Devil on 16/8/13.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

class WLStatusListViewModel: NSObject {
    //保存微博数据数组
    lazy var dataArray:[WLStatusViewModel] = [WLStatusViewModel]()
    
}

extension WLStatusListViewModel{
    //请求首页数据
    func loadData(isPullup: Bool, finish:(isSuccess: Bool) -> ()){
        
        var sinaceId: Int64 = 0
        var maxId: Int64 = 0
        
        if isPullup {
            maxId = dataArray.last?.statusModel?.id ?? 0
            if maxId > 0 {
                maxId -= 1
            }
        }else {
            sinaceId = dataArray.first?.statusModel?.id ?? 0
        
        }
        
        
    
        WLNetworkingTools.sharedTools.homeLoadData(sinaceId,maxId: maxId,success: {(response)->() in
        
//            print("请求成功:\(response)")
            //判断response 是否为nil
            guard let res = response?["statuses"] as? [[String:AnyObject]] else{
                finish(isSuccess: false)
                return
            }
            //创建一个临时可变数组
            var tempArray:[WLStatusViewModel] = [WLStatusViewModel]()
            //遍历数组
            for dic in res {
                let status = WLStatusModel(dict: dic)
                let statusViewModel = WLStatusViewModel(statusModel: status)
                //添加模型
                tempArray.append(statusViewModel)
            }
            
            if isPullup {
                self.dataArray = self.dataArray + tempArray
            
            }else {
                self.dataArray = tempArray + self.dataArray
            
            }
            
//            self.dataArray = tempArray
//            print(self.dataArray.count)
            finish(isSuccess: true)
        }){(error)->() in
            print("请求失败:\(error)")
            finish(isSuccess: false)
        
        }
    }
}
